//
// Created by enemy1205 on 2021/8/10.
//
#include "control.h"

/*登录*/
void Control::Login(string file_name, int type) {
    ifstream ifs;
    ifs.open(file_name, ios::in);
    if (!ifs.is_open()) {
        cout << "核验文件不存在" << endl;
        ifs.close();
        return;
    }
    if (type == 1)    //学生登录
    {
        cout << "请输入你的学号" << endl;
        cin >> id;
    } else if (type == 2) //教师登录
    {
        cout << "请输入你的职工号" << endl;
        cin >> id;
    }

    cout << "请输入用户名：" << endl;
    cin >> name;

    cout << "请输入密码： " << endl;
    cin >> pwd;
    auto * manager=new Manager;
    manager->count_num();
    if (type == 1) {
        //学生登录验证
        if (Stu_id_check(manager)) {
            getchar();
            printf("\33[2J");
            auto *person = new student_re;
            person->ID_init(name, pwd, id);
            person->operMenu(name,manager);
            delete person;
            getchar();
        }
    } else if (type == 2) {
        //教师登录验证
        if (Tea_id_check(manager)) {
            getchar();
            printf("\33[2J");
            auto *person = new teacher;
            person->ID_init(name, pwd, id);
            person->operMenu(name,manager);
            delete person;
            getchar();
        }
    } else if (type == 3) {
        //管理员登录验证
        if (Man_id_check()) {
            getchar();
            printf("\33[2J");
            auto *person = new Manager;
            person->ID_init(name, pwd);
            person->operMenu(name);
            delete person;
            getchar();
        }
    }
}
//TODO student.txt/teacher.txt第一行账户无法登录
/*身份核验*/
bool Control::Stu_id_check(Manager* manager){
    for (auto it=manager->get_Stu().begin();it!=manager->get_Stu().end();++it) {
//        cout<<it->get_num()<<"  "<<it->get_name()<<"  "<<it->get_passport()<<endl;
        if(it->get_num()==id&&it->get_name()==name&&it->get_passport()==pwd) return true;
    }
    cout << "验证登录失败!" << endl;
    return false;
}

bool Control::Tea_id_check(Manager* manager){
    for (auto it=manager->get_Tea().begin();it!=manager->get_Tea().end();++it) {
        if(it->get_num()==id&&it->get_name()==name&&it->get_passport()==pwd) return true;
    }
    cout << "验证登录失败!" << endl;
    return false;
}
bool Control::Man_id_check() {
    ifstream ifs;
    ifs.open(ADMIN_FILE, ios::in);
    if (!ifs.is_open()) {
        cout << "核验文件不存在" << endl;
        ifs.close();
        return false;
    }
    string fName;
    string fPwd;
    ifs >> fName;
    ifs >> fPwd;
    if (name == fName && pwd == fPwd) {
        cout << "验证登录成功!" << endl;
        return true;
    } else {
        cout << "验证登录失败!" << endl;
        return false;
    }
}
